package yi.Note.ui;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.OpenableColumns;
import yi.Note.R;
import yi.Note.data.provider.NoteContent;
import yi.Note.util.Utility;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: b251
 * Date: 13-12-14
 * Time: 下午7:08
 */
public class EditorActivity extends Activity implements AttGallery.AttHandlerCallback{
    public static final String INTENT_NEW = "yi.note.ACTION_NEW";
    public static final String INTENT_EDIT = "yi.note.ACTION_EDIT";

    public static final String[] ACCEPTABLE_ATTACHMENT_SEND_UI_TYPES = new String[] {
            "image/*",
            "video/*",
    };
    private static final int ACTIVITY_REQUEST_PICK_ATTACHMENT = 1;

    private static final String[] ATTACHMENT_META_SIZE_PROJECTION = {
            OpenableColumns.SIZE
    };
    private static final int ATTACHMENT_META_SIZE_COLUMN_SIZE = 0;

    private AttGallery mAttsView;
    private AttGallery.EditCacheAdapter mAdapter;
    private boolean mPickingAttachment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_content);
        mAttsView = (AttGallery)findViewById(R.id.content_att);

//        Uri uri = Uri.parse("content://media/external/images/media/5728");
//        NoteContent.Attachment attachment = new NoteContent.Attachment();
//        attachment.mAttUri = uri.toString();
//        attachment.mNoteId = -1; //TODO
//        attachment.mMimeType = Utility.inferMimeTypeForUri(this, uri);

        mAdapter = mAttsView.createEditAdatper(new NoteContent.Attachment[]{});
        mAttsView.setHandlerCallback(this);
        mAttsView.load(this, mAdapter);
        mPickingAttachment = false;
    }


    @Override
    public void onAddAttachment() {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        i.setType(ACCEPTABLE_ATTACHMENT_SEND_UI_TYPES[0]);
        mPickingAttachment = true;
        startActivityForResult(
                Intent.createChooser(i, getString(R.string.choose_attachment_dialog_title)),
                ACTIVITY_REQUEST_PICK_ATTACHMENT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mPickingAttachment = false;
        if (data == null) {
            return;
        }
        NoteContent.Attachment att = loadAttachmentInfo(data.getData());
        mAdapter.addAtt(att);
        //att.save(this);
    }

    @Override
    public void onDelAttachment(NoteContent.Attachment a) {

    }

    @Override
    public void onClickAttachment(NoteContent.Attachment a) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        i.setType(a.mMimeType);
        i.setData(Uri.parse(a.mAttUri));
        startActivity(i);
    }

    private NoteContent.Attachment loadAttachmentInfo(Uri uri) {
        long size = -1;
        ContentResolver contentResolver = getContentResolver();

        // Load name & size independently, because not all providers support both
        final String name = Utility.getContentFileName(this, uri);
        Cursor metadataCursor = contentResolver.query(uri, ATTACHMENT_META_SIZE_PROJECTION,
                null, null, null);
        if (metadataCursor != null) {
            try {
                if (metadataCursor.moveToFirst()) {
                    size = metadataCursor.getLong(ATTACHMENT_META_SIZE_COLUMN_SIZE);
                }
            } finally {
                metadataCursor.close();
            }
        }

        // When the size is not provided, we need to determine it locally.
        if (size < 0) {
            // if the URI is a file: URI, ask file system for its size
            if ("file".equalsIgnoreCase(uri.getScheme())) {
                String path = uri.getPath();
                if (path != null) {
                    File file = new File(path);
                    size = file.length();  // Returns 0 for file not found
                }
            }
        }

        contentResolver.getType(uri);

        NoteContent.Attachment attachment = new NoteContent.Attachment();
        attachment.mAttUri = uri.toString();
        attachment.mNoteId = NoteContent.ID_NONE;
        attachment.mId = NoteContent.ID_NONE;
        attachment.mMimeType = Utility.inferMimeTypeForUri(this, uri);
        return attachment;
    }
}
